package com.sudo.common.utils;

import java.util.Random;
import java.util.UUID;

/**
 * @author 为道日损 2020-04-28 15:18
 */
public class RandomUtil {
    private RandomUtil(){}
    private static final Random random = new Random();

    public static String getRandomInt(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }
    public static String getUUID() {
        return UUID.randomUUID().toString().trim();
    }

    public static String getRandomStr(int n) {
        //3、定义一个字符串变量记录生成的随机字符
        StringBuilder code = new StringBuilder();
        //2、定义一个for循环，循环n次，依次生成随机字符
        for (int i = 0; i < n; i++) {
            //i=0 1 2
            //3、生成一个随机字符，英文大、小写 数字（0 1 2 ）
            int type = random.nextInt(3);//0 1 2
            switch (type) {
                case 0:
                    //大写字符（A 65-Z 65+25）
                    char ch = (char) (random.nextInt(26) + 65);
                    code.append(ch);
                    break;
                case 1:
                    //小写字符（a 97-z 97+25）
                    char ch1 = (char) (random.nextInt(26) + 97);
                    code.append(ch1);
                    break;
                default:
                    //数字字符
                    code.append(random.nextInt(10));//0-9
                    break;
            }
        }
        return code.toString();
    }
}
